﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace JobbJakt
{
    public partial class CompanyContact
    {
        /// <summary>
        /// Konstruktor som tar in alla parametrar
        /// </summary>
        public CompanyContact(string companyname, string firstname,
            string lastname, string position, string email, string phone, string city, int companycontactid = 0)
        {
            CompanyName = companyname;
            FirstName = firstname;
            LastName = lastname;
            Position = position;
            Email = email;
            Phone = phone;
            City = city;
        }
        /// <summary>
        /// CREATE:
        /// Skickar in en CompanyContact och returnerar antal affected rows.
        /// </summary>
        /// <param name="companyContact"></param>
        /// <returns>Antal ändrade rader</returns>
        public static int AddNewCompanyContact(CompanyContact companyContact)
        {
            var query = JobbJaktDB.CompanyContacts.Add(companyContact);
            int nmbrOfAffectedRows = JobbJaktDB.Save();
            return nmbrOfAffectedRows;
        }
        /// <summary>
        /// READ:
        /// Metod där man skickar in ett companycontactID och får en companycontact tillbaka
        /// </summary>
        /// <param name="companycontactid"></param>
        /// <returns>CompanyContact</returns>
        public static CompanyContact GetCompanyContact(int companycontactid)
        {
            var query = JobbJaktDB.CompanyContacts
                .SingleOrDefault(c => c.CompanyContactID == companycontactid);
            return query;
        }
        /// <summary>
        /// Skickar in ett companycontactID och returnerar en companycontactlista.
        /// </summary>
        /// <param name="companycontactID"></param>
        /// <returns>companycontacts list</returns>
        public static List<CompanyContact> GetCompanyContacts(int companycontactID)
        {
            var query = JobbJaktDB.CompanyContacts
                .Where(n => n.CompanyContactID == companycontactID).ToList();
            return query;
        }
        /// <summary>
        /// UPDATE:
        /// Skickar in en companycontactID och returnerar antal påverkade rader.
        /// </summary>
        /// <param name="companycontactid"></param>
        /// <returns>nbrOfAffectedRows</returns>

        ///
        public static int UpdateCompanyContact(CompanyContact companyContact)
        {
            JobbJaktDB.CompanyContacts.AddOrUpdate(companyContact);
            return JobbJaktDB.Save();
        }
        /// <summary>
        /// DELETE:
        /// Skickar in ett companycontactId och returnerat antal påverkade rader.
        /// </summary>
        /// <param name="companycontactid"></param>
        /// <returns>nbrOfAffectedRows</returns>
        public static int DeleteCompanyContact(int companycontactid)
        {
            var context = JobbJaktDB.Context;
            var query = context.CompanyContactSet.FirstOrDefault(
                n => n.CompanyContactID == companycontactid);

            context.CompanyContactSet.Remove(query);
            int nbrOfAffectedRows = context.SaveChanges();
            return nbrOfAffectedRows;
        }
        /// <summary>
        /// TO STRING:
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("KontaktpersonID: {0}/r/n" +
                                 "Företagsnamn: {1}/r/n" +
                                 "Förnamn: {2}/r/n" +
                                 "Efternamn: {3}/r/n" +
                                 "Position: {4}/r/n" +
                                 "Stad: {5}/r/n" +
                                 "Epost: {6}/r/n" +
                                 "Telefonnummer: {7}/r/n",
                CompanyContactID, CompanyName, FirstName, LastName, Position, City, Email, Phone);
        }
    }
}
